/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.backing;

import com.ez2find.manager.RegisterUserManagerLocal;
import com.ez2find.model.RegisterUser;
import com.ez2find.util.SessionContext;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Owner
 */
public class LoginBean {

    @EJB
    private RegisterUserManagerLocal ejbUserManager;
    private String email;
    private String password;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public String login(){
        try{
            if ((email.length() < 4) || !email.contains("@") || !email.contains(".")) {
                FacesMessage msg = new FacesMessage("Please enter a valid email");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            RegisterUser identity = ejbUserManager.findLoginUser(getEmail(),getPassword());
            if (identity == null){
                FacesMessage msg=new FacesMessage(FacesMessage.SEVERITY_ERROR, "No such user, please try again", "The authentication failed please check the user name and password and try again");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
            if (identity.getStatus().equals("active")){
                SessionBean sessionBean=(SessionBean) SessionContext.getAttribute("sessionBean");
                sessionBean.getEjbSession().setOwner(identity);
                //if(identity.getDefaultSite()!=null)
                //    sessionBean.getEjbSession().setCurrentSite(identity.getDefaultSite());
                identity.setLoggedDate(new Date(System.currentTimeMillis()));
                ejbUserManager.update(identity, false);
                return "success";
            }
            else{
                FacesMessage msg=new FacesMessage(FacesMessage.SEVERITY_ERROR, "Your Account not active, please active your account from email recieved", "The Login failed please check the email received from ez2find for active your account and try again");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }
        }catch(Exception ex){
            ex.printStackTrace();
            FacesMessage msg=new FacesMessage(FacesMessage.SEVERITY_ERROR, "No such user or error ocurred, please try again", "The authentication failed please check the user name and password and try again");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
    }
    
    public String logout(){
        try{
            SessionBean sessionBean=(SessionBean) SessionContext.getAttribute("sessionBean");
            sessionBean.getEjbSession().setOwner(null);
            sessionBean.setPagePostsUser(0);
            return "success";
        }catch(Exception ex){
            ex.printStackTrace();
            FacesMessage msg=new FacesMessage(FacesMessage.SEVERITY_ERROR, "The logout failed", "The logout failed");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
    }
    
    public String logOut(){
        try{
            SessionBean sessionBean=(SessionBean) SessionContext.getAttribute("sessionBean");
            sessionBean.getEjbSession().setOwner(null);
            sessionBean.setPagePostsUser(0);
            return "logout";
        }catch(Exception ex){
            ex.printStackTrace();
            FacesMessage msg=new FacesMessage(FacesMessage.SEVERITY_ERROR, "The logout failed", "The logout failed");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
    }
    
    public String logoutCP(){
        try{
            SessionBean sessionBean=(SessionBean) SessionContext.getAttribute("sessionBean");
            sessionBean.getEjbSession().setOwner(null);
            sessionBean.setPagePostsUser(0);
            SessionContext.removeAttribute("UsersBean");
            SessionContext.removeAttribute("categorieBean");
            SessionContext.removeAttribute("FlagedPostsBean");
            SessionContext.removeAttribute("siteBean");
            SessionContext.removeAttribute("BackupBean");
            return "success";
        }catch(Exception ex){
            ex.printStackTrace();
            FacesMessage msg=new FacesMessage(FacesMessage.SEVERITY_ERROR, "The logout failed", "The logout failed");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
    }
    
    public boolean isHasMessages() {
	return FacesContext.getCurrentInstance().getMessages().hasNext();
    }
    
    public void setToNull(){
        email = null;
        password = null;
    }
}
